from django.shortcuts import render

# Create your views here.
#  url(r'^qq/authorization/$', views.QQAuthURLView.as_view()),
from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from .exceptions import QQAPIException
from .models import OAuthQQUser
from oauth.utils import OAuthQQ
from . import serializers

from rest_framework_jwt.views import api_settings


class QQAuthUserView(GenericAPIView):
    """用户扫码登陆的回调处理"""
    # 指定序列化器
    serializer_class = serializers.QQAuthUserSerializer

    def get(self, request):
        # 提取code请求参数 路由地址
        print(request.query_params)
        code = request.query_params.get("code")
        if not code:
            return Response({"message": "缺失code"}, status=status.HTTP_400_BAD_REQUEST)
        # 创建QQ登录工具对象
        oauth = OAuthQQ()
        try:
            # 使用code向QQ服务器请求access_token
            access_token = oauth.get_access_token(code)

            # 使用access_token向QQ服务器请求open_id
            openid = oauth.get_openid(access_token)
        except QQAPIException as e:
            return Response({"message": "QQ服务器异常"}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
        # 使用openid查询该QQ用户是否在美多商城中绑定过用户
        try:
            oauth_user = OAuthQQUser.objects.get(openid=openid)
        except OAuthQQUser.DoesNotExist:
            # 如果 openid没有绑定美多商城用户 创建用户绑定到openid
            access_token_openid = OAuthQQ.generate_save_user_token(openid)
            return Response({"access_token": access_token_openid})
        else:
            # 如果 openid已绑定美多商城用户 直接生产JWt_token 并返回
            jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
            jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

            # 获取关联openid的user
            user = oauth_user.user

            payload = jwt_payload_handler(user)
            token = jwt_encode_handler(payload)

            # 向前端响应token user_id username
            return Response({
                "token": token,
                "user_id": user.id,
                "username": user.username

            })

    def post(self, request):
        """
        给openid绑定用户数据
        :param request:
        :return:
        """
        # 获取序列化器 注册的数据都在post请求的请求体里面
        serializer = self.get_serializer(data=request.data)
        # 开启校验
        serializer.is_valid(raise_exception=True)
        # 保存校验的数据 create会返回user
        user = serializer.save()
        # 生成JWT token响应
        # 如果 openid已绑定美多商城用户 直接生产JWt_token 并返回
        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)

        # 向前端响应token user_id username
        return Response({
            "token": token,
            "user_id": user.id,
            "username": user.username

        })


# url(r"^qq/authorization/$", views.QQAuthURLView.as_view())
class QQAuthURLView(APIView):
    """
    调用QQ扫码登陆界面的链接给用户
    """

    def get(self, request):
        """
        提供用于qq登录的url
        """
        # (逻辑)生成QQ扫码登陆的链接
        # login_url = oauth.get_qq_login_url()

        # (过程) 如何生成QQ扫码登陆的链接

        # 获取到next参数 为了实现将来从哪里进入的登陆界面
        # QQ登陆成功后就回到哪里
        next = request.query_params.get('next')

        # 创建qq登录的工具对象
        oauth = OAuthQQ(state=next)

        # 生成QQ扫码登陆的链接(逻辑)
        login_url = oauth.get_qq_login_url()
        print("login_url:",login_url)
        return Response({'login_url': login_url})
